/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.proteu.util;

/**
 *
 * @author eros
 */

import java.text.SimpleDateFormat;
import java.util.*;


public class CalcBetweenDates{
    public static SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyyy");

    public static Long returnDaysBtDates(Date dtInicio,Date dtFinal){
        long result = 0;
        if(dtInicio != null && dtFinal != null && dtInicio.before(dtFinal)){
             long mills_per_day = 1000 * 60 * 60 * 24;
            result = ( dtFinal.getTime() - dtInicio.getTime() ) / mills_per_day;
            return result;
        }
       return result;
    }

    public static int returnYearsBtDates(Date dtInicio,Date dtFinal){
        if(dtInicio != null && dtFinal != null && dtInicio.before(dtFinal)){
            return  new Long(returnDaysBtDates(dtInicio,dtFinal)/365).intValue() ;
        }else{
            return 0;
        }
        
    }

    public static String returnYearsMonthAndDays(Date dtInicio,Date dtFinal){
        String anos = (new Long(returnYearsBtDates(dtInicio, dtFinal))).toString();
        format = new SimpleDateFormat("dd");
        Integer day1 = (new Integer(format.format(dtInicio)));
        Integer day2 = (new Integer(format.format(dtFinal)));
        Integer days = 0;
        if(day1 > day2){
            days = day1 - day2;
        }else{
            days = day2 - day1;
        }
        Integer meses = 0;
        format = new SimpleDateFormat("MM");
        Integer meses1 = (new Integer(format.format(dtInicio)));
        Integer meses2 = (new Integer(format.format(dtFinal)));

       if(meses1 > meses2){
            meses = meses1 - meses2;
        }else{
            meses = meses2 - meses1;
        }

        
       return anos.toString()+"a"+meses.toString()+"m"+days.toString()+"d";
    }

  
}
